Rem Travel Center Test Card
Rem ------------------------------------------------------------------
Rem Copyright (C) 2012 TEAM
Rem ------------------------------------------------------------------

Rem  A simple basic card for testing the Travel Center Terminal

Option Explicit ' Disable use of undefined variable names

#Include PassengerCard.DEF
#Include DEALER.KEY
#Include TRAVELCENTER.KEY
#Include ../Includes/CommonTypes.DEF
#Include ../Includes/CommonFunctions.DEF

#Include ../Includes/BasicCardCommands.DEF
#Include ../Includes/BasicCardMisc.DEF

Declare ApplicationID = ApplicationName$

#IfDef MultiAppBasicCard

  #Include COMPONNT.DEF

  Dir "\"
    Application "DefaultApp" ' Make this the Default Application
      Lock=Execute:Always

    Key "Key0" Usage=kuSMEnc Algorithm=AlgSingleDesCrc
      Lock=Never
      LCIndexedKey (ltCompileTime, 0) ' Key 0 from TRAVELCENTER.KEY

    Key "Key1" Usage=kuSMEnc Algorithm=AlgSingleDesCrc
      Lock=Never
      LCIndexedKey (ltCompileTime, 1) ' Key 1 from DEALER.KEY

    ' Restrict protocol to T=1 by use of ATR file (required for balance reader)
    File "ATR" Lock Read: Always
      ATR (T=1)

  End Dir

  #Heap 300

#Else

  Disable Encryption &H11

#EndIf

Rem  Permanent data

Eeprom Personalised        = False
Eeprom eepromData as Travel
Eeprom PINErrors

Rem  EEPROM shadow data, to ensure that Balance is always valid
Rem
Rem  See "1.4.4 Permanent Data" in the BasicCard manual.

Eeprom Committed = False

Rem  Public data (re-initialised when card is reset)

Rem Public PINVerified = False
Rem Public MasterPINVerified = False

Rem  Subroutine declaration

Declare Sub       ChangeBalance (NewBalance As Long)
Declare Sub       CheckAlgorithm()
Declare Function  GetKeyNumber@()

Rem  Start-up code: clean up if an EEPROM write was interrupted
Rem
Rem  See "1.4.4 Permanent Data" in the BasicCard manual.

Rem Prueft ob Reisender an Bord. result = 0 wenn ja, ansonsten 1
Function CheckOnBoard( result )
  if eepromData.TravelPassengerOnBoard = 1 then
      result = 0
  else
      result = 1
  end if
End Function

Rem Prueft ob Reisender im Buchungszeitraum ist. result = 0 wenn ja, ansonsten 1
Function CheckInTime( result, currentTime as Date )
  if IsInDuration( eepromData.TravelStartDate, eepromData.TravelEndDate, currentTime ) = -1 then
    result = 1
  else
    result = 0
  end if
End Function

Rem Prueft ob Reisender das Zimmer hat. result = 0 wenn ja, ansonsten 1
Function CheckRoom( result, roomnumber )
  if eepromData.TravelCabineNumber = roomnumber then
      result = 0
  else
      result = 1
  end if
End Function

Command &H80 &H40 OpenDoor( roomnumber as Integer, travelName as String*25, currentTime as Date )
  If Not Personalised Then SW1SW2 = swNotPersonalised : Exit
  Private r as Integer
  
  if travelName <> eepromData.TravelName then SW1SW2 = swWrongShipName : Exit

  Call CheckOnBoard( r )
  if r > 0 then SW1SW2 = swNotOnBoard : Exit
  
  Call CheckInTime( r, currentTime )
  if r > 0 then SW1SW2 = swNotInTime : Exit
  
  Call CheckRoom( r, roomnumber )
  if r > 0 then SW1SW2 = swWrongRoomNumber : Exit
End Command


Command &H80 &H00 PersonaliseCard( data as Travel )

  Call CheckAlgorithm()
  
  If GetKeyNumber@() <> 0 Then SW1SW2 = swIssuingKeyRequired : Exit

  Personalised    = False
  eepromData = data  
  Personalised    = True
  
End Command


Command &H80 &H02 GetCardData ( data as Travel )

  If Not Personalised Then SW1SW2 = swNotPersonalised : Exit
  data = eepromData  
   

End Command

Command &H80 &H04 ResetCardData ()
  
  Personalised = False

End Command


Rem  Sub CheckAlgorithm()
Rem
Rem  Check that the appropriate encryption algorithm is enabled:
Rem
Rem  Compact BasicCard:          AlgSgLfsrCrc &H12 (SG-LFSR with CRC)
Rem  Enhanced BasicCard:         AlgSingleDes &H21 (Single DES)     
Rem  Professional BasicCard:     AlgSingleDesCrc &H23 (Single DES with CRC) or greater
Rem  MultiApplication BasicCard: AlgSingleDesCrc &H23 (Single DES with CRC) or greater

Sub CheckAlgorithm()

#IfDef CompactBasicCard
  If Algorithm <> AlgSgLfsrCrc Then SW1SW2 = swEncryptionRequired : Exit
#ElseIfDef EnhancedBasicCard
  If Algorithm <> AlgSingleDes Then SW1SW2 = swEncryptionRequired : Exit
#ElseIfDef ProfessionalBasicCard
  If Algorithm < AlgSingleDesCrc Then SW1SW2 = swEncryptionRequired : Exit
#Else
  If Algorithm < AlgSingleDesCrc Then SW1SW2 = swEncryptionRequired : Exit
#EndIf

End Sub

Rem  Function GetKeyNumber@()
Rem
Rem  Return the number of the active key

Function GetKeyNumber@()

   GetKeyNumber@ = KeyNumber

End Function


Command &H80 &H20 ToggleCheckInStatus (Status as Byte) 
    If eepromData.TravelPassengerOnBoard = 1 Then
      REM CheckOut
      eepromData.TravelPassengerOnBoard = 0
    Else
      REM CheckIn
      eepromData.TravelPassengerOnBoard = 1
    End If
    Status = eepromData.TravelPassengerOnBoard
End Command

Command &H80 &H24 ResetRoomNumber ()
  eepromData.TravelCabineNumber = -1
End Command

Command &H80 &H26 SetRoomNumber( room )
  eepromData.TravelCabineNumber = room
End Command

Command &H80 &H28 GetRoomNumber( room )
    room = eepromData.TravelCabineNumber
End Command

Command &H80 &H30 CardPersonalised ( cardPersonalised As Byte )
  If ( Personalised ) Then
    cardPersonalised = 1
  Else
    cardPersonalised = 0
  End If
End Command